//
//  ViewController.swift
//  TestUI
//
//  Created by luo luo on 2023/10/18.
//

import UIKit
//import TGSPublic
//import EPUBKit
//import FolioReaderKit


extension String {
    
    func fetchContentOfThreeBrace() -> String {
        // 创建一个正则表达式来匹配被三个大括号包围的文本
        let pattern = #"\{\{\{(.*?)\}\}\}"#
        do {
            // 编译正则表达式
            let regex = try NSRegularExpression(pattern: pattern, options: [])
            
            // 定义搜索范围为整个字符串
            let nsRange = NSRange(self.startIndex..<self.endIndex, in: self)
            
            // 查找所有匹配项
            let matches = regex.matches(in: self, options: [], range: nsRange)
            
            // 遍历所有匹配项并打印结果
            for match in matches {
                if let range = Range(match.range(at: 1), in: self) {
                    let matchedText = self[range]
//                    print("正则匹配到的总结文字是：\(matchedText)")
                    return "\(matchedText)"
                }
            }
            
            if let range = self.range(of: "{{{"),let range2 = self.range(of: "}}}") {
                let  tempStr2 = String(self[range.upperBound..<range2.lowerBound])
                print("正则未获取到 fetch by handle is \(String(tempStr2))")
            }else{
                print("正则未匹配到的三个大括号 matches是：\(matches)")
            }
        } catch let error {
            print("正则表达式失败: \(error.localizedDescription)")
        }
        return ""
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
//        self.view.addSubview(self.displayView)
//        self.view.sendSubviewToBack(self.displayView)
//        let temp = UICollectionView.init()
       
        
        self.button.isHidden = false
       
        

      

        // 假设已录制用户发音和标准音频，分别为 file1 和 file2
        let fileURL1 = Bundle.main.url(forResource: "user_audio", withExtension: "wav")!
        let fileURL2 = Bundle.main.url(forResource: "standard_audio", withExtension: "wav")!

        let file1 = try! AKAudioFile(forReading: fileURL1)
        let file2 = try! AKAudioFile(forReading: fileURL2)

        // 1. 提取 MFCC 特征并计算相似度（使用外部库或自定义实现）
        let mfcc1 = extractMFCC(from: file1)
        let mfcc2 = extractMFCC(from: file2)
        let mfccSimilarity = calculateSimilarity(mfcc1, mfcc2)

        // 2. 计算语调相似度（音高分析）
        let pitch1 = extractPitch(from: file1)
        let pitch2 = extractPitch(from: file2)
        let pitchSimilarity = calculatePitchSimilarity(pitch1, pitch2)

        // 3. 计算流利度分数（语速分析）
        let tempo1 = extractTempo(from: file1)
        let tempo2 = extractTempo(from: file2)
        let fluencyScore = calculateFluency(tempo1, tempo2)

        // 4. 综合得分（加权平均）
        let finalScore = 0.4 * mfccSimilarity + 0.3 * pitchSimilarity + 0.3 * fluencyScore

        print("Final pronunciation score: \(finalScore * 100)%")

        print("添加master日志")
        print("添加master日志2")
        
    }
    
    func findDifferences(from A: String, to B: String) -> (added: String, removed: String) {
        var added = ""
        var removed = ""
        
        let maxLength = max(A.count, B.count)
        
        for i in 0..<maxLength {
            let indexA = A.index(A.startIndex, offsetBy: i, limitedBy: A.endIndex)
            let indexB = B.index(B.startIndex, offsetBy: i, limitedBy: B.endIndex)
            
            let charA = indexA.flatMap { A[$0] }
            let charB = indexB.flatMap { B[$0] }
            
            if charA != charB {
                if let charB = charB {
                    added.append(charB)
                }
                if let charA = charA {
                    removed.append(charA)
                }
            }
        }
        
        return (added, removed)
    }

   


   

    
  
   

   
    lazy var button: UIButton = {
        let view = UIButton()
        view.setTitle("test", for: UIControl.State.normal)
        view.backgroundColor = UIColor.red
        view.frame = CGRect.init(x: 50, y: 100, width: 100, height: 50)
        view.addTarget(self, action: #selector(testAction), for: UIControl.Event.touchUpInside)
        self.view.addSubview(view)
        return view
    }()
    @objc func testAction() -> Void {
        // 示例数组
       
    }
    
    
    
    
   
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

    }
    
    
    
   
    
    
   

}

